﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waiter.Common;
using Waiter.Controler;

namespace Waiter.View
{
    public partial class frmEditBill : DevComponents.DotNetBar.Office2007Form
    {
        public frmEditBill()
        {
            InitializeComponent();
        }
        public void fsLoadCmbStaff()
        {
            StaffCtrl staffCtrl = new StaffCtrl();
            List<STAFF> staffList = staffCtrl.fsGetAllStaffs();
            if (staffList == null || staffList.Count == 0)
            {
                return;
            }

            List<ComboboxItem> itemList = (from s in staffList
                                           select new ComboboxItem()
                                           {
                                               Text = s.NAME,
                                               Value = s.CODE
                                           }).ToList();
            this.cmbStaff.ValueMember = CommonConstants.CMB_VALUE;
            this.cmbStaff.DisplayMember = CommonConstants.CMB_TEXT;
            this.cmbStaff.DataSource = itemList;

            CommonCtrl.fsCmbSetSelectedValue(this.cmbStaff, CommonUtils.fsGetOPID());
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private string billCode = null;
        public void fsInputData(string code)
        {
            billCode = code;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string staffCode = CommonCtrl.fsCmbGetSelectedValue(this.cmbStaff);
            if (staffCode == string.Empty || staffCode == null)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Bạn phải chọn một nhân viên!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.cmbStaff.Focus();
                return;
            }

            if (this.txtNote.Text.Trim().Length == 0)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Bạn phải nhập nội dung ghi chú!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtNote.Focus();
                return;
            }

            try
            {
                BillCtrl billCtrl = new BillCtrl();
                BILL item = billCtrl.fsFindBillBycode(billCode);
                item.OPID = staffCode;
                item.NOTE = txtNote.Text;

                CommonGlobalVariable.globalDataContext.SubmitChanges();
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "frmEditBill.btnSave_Click",
                    CommonUtils.fsParamListToString(new object[] { billCode }),
                    ex.ToString());
            }
        }

        private void frmEditBill_Load(object sender, EventArgs e)
        {
            fsLoadCmbStaff();

            BillCtrl billCtrl = new BillCtrl();
            BILL item = billCtrl.fsFindBillBycode(billCode);
            if (item == null)
            {
                this.btnSave.Enabled = false;
                return;
            }

            this.txtCreateDate.Text = item.CREATE_DATE.ToString();
            this.txtTableName.Text = item.ORDER_COUPON.TABLE.TABLE_NAME;
            this.txtMoney.Text = CommonUtils.fsCreateMoneyString(item.TOTAL_AMOUNT);
            this.txtNote.Text = item.NOTE;
            CommonCtrl.fsCmbSetSelectedValue(this.cmbStaff, item.OPID);
        }
    }
}